package com.exadel.googleregistrator.dao.impl;

import com.exadel.googleregistrator.dao.AccountDAO;
import com.exadel.googleregistrator.entities.Account;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 09.08.12
 * Time: 16:46
 */
@Repository
public class AccountDAOImpl implements AccountDAO {

    @PersistenceContext
    EntityManager entityManager;

    public EntityManager getEntityManager() {
         return entityManager;
    }

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    @Transactional
    public Account readAccount(int id) {
        return entityManager.find(Account.class, id);
    }

    @Transactional
    public List<Account> getAccounts() {
        //TODO write query
        return entityManager.createQuery(""/*"from " + clazz.getName()*/).getResultList();
    }

    @Transactional
    public void createAccount(Account account) {
        entityManager.persist(account);
    }

    @Transactional
    public void deleteAccount(Account account) {
        entityManager.remove(account);
    }

    @Transactional
    public void updateAccount(Account account){
        entityManager.merge(account);
    }
}
